using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using netCoreStudy.Entities;
using netCoreStudy.Entities.Base;
using Serilog;
using Serilog.Events;

namespace netCoreStudy
{
	public class Program
	{
		public static void Main(string[] args)
		{


			/*
			 * 配置一下serilog 替换 默认 的 log
			 * Data Source=192.168.1.140;Database=AcquisitionSystem;User ID=root;Password=root;pooling=true;CharSet=utf8;port=3306;sslmode=none
			 */
			Log.Logger = new LoggerConfiguration()
				.MinimumLevel.Debug()
				.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
				.Enrich.FromLogContext()
				.WriteTo.Console()
				.WriteTo.File(
					path: $@"{AppContext.BaseDirectory}\Logs\Server-Log-.log",
					outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}",
					rollingInterval: RollingInterval.Day,
					fileSizeLimitBytes: 10485760,
					rollOnFileSizeLimit: true,
					retainedFileCountLimit: 10
				)
				.CreateLogger();
			try
			{
				Log.Information($@"程序启动了 ,bath 为 {AppContext.BaseDirectory}");
				CreateHostBuilder(args).Build().Run();
			}
			catch (Exception e)
			{
				Log.Fatal(e, "程序出现错误了！");
			}
			finally
			{
				Log.CloseAndFlush();
			}
		}

		public static IHostBuilder CreateHostBuilder(string[] args) =>
			Host.CreateDefaultBuilder(args)
				// 使用 autofac 依赖注入
				.UseServiceProviderFactory(new AutofacServiceProviderFactory())
				.UseSerilog()
				.ConfigureWebHostDefaults(webBuilder =>
				{
					webBuilder
						.UseUrls("http://*:8082")
						.UseStartup<Startup>();
				});
	}
}